<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/9/16
 * Time: 16:44
 */

namespace app\service;

use think\Db;

/**
 * 定时服务服务层
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class CrondService
{
    /**
     * 定时扣除微信支付宝手续费.
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     */
    public static function CrondCost(){
        $today_time_start =  date('Y-m-d 00:00:00', time());
            //date('Y-m-d 00:00:00');
        $today_time_end = TIMESTAMP;
        $where=[
            ['create_time','>',$today_time_start],
            ['create_time','<',$today_time_end],
            ['type','=',2]
        ];
        $data=Db::name('log_income')->where($where)->select();
        $wx_total=0;
        $ali_total=0;
        foreach ($data as $v)
        {
            if ($v['pay']==1)
            {
                $wx_total+=$v['total_price'];
            }else{
                $ali_total+=$v['total_price'];
            }
        }

        $msg_wx='今日微信手续费为0';
        $msg_ali='今日支付宝手续费为0';
        if($wx_total>0)
        {
            $wx_cost=$wx_total*0.006;
            $msg_wx='微信手续费扣除成功';
            $data=[
                'title'=>'微信手续费',
                'explain'=>'自动扣除',
                'price' =>$wx_cost,
                'pay_type' =>1
            ];
            Db::name('log_cost')->insert($data);
        }
        if($ali_total>0)
        {
            $ali_cost=$ali_total*0.006;
            $msg_ali='支付宝手续费扣除成功';
            $data=[
                'title'=>'支付宝手续费',
                'explain'=>'自动扣除',
                'price' =>$ali_cost,
                'pay_type' =>2
            ];
            Db::name('log_cost')->insert($data);
        }
        return DataReturn($msg_wx.'.'.$msg_ali,0);
    }
}